package com.yaoandw.test;

import java.util.concurrent.ThreadPoolExecutor;

import com.yaoandw.crawler.LinkQueue;

public class Mtask implements Runnable {
	private Mtest test;
	public Mtask(Mtest test){
		this.test  =test;
	}
	public void run() {
//		System.out.println("thread id:"+Thread.currentThread().getId());
		String visitUrl = null;
		if ((visitUrl = LinkQueue.unVisitedUrlDeQueue()) != null) {
			LinkQueue.addVisitedUrl(visitUrl);
			System.out.println(Thread.currentThread().getId()+","+visitUrl);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if("a".equals(visitUrl))
				addUnvisitedUrl("c");
			else if("b".equals(visitUrl))
				addUnvisitedUrl("d");
			else if("c".equals(visitUrl)){
				addUnvisitedUrl("e");
				addUnvisitedUrl("f");
			}
			
			else if("d".equals(visitUrl))
				addUnvisitedUrl("f");
			else if("e".equals(visitUrl))
				addUnvisitedUrl("f");
		}
//		addUnvisitedUrl(null);
	}
	private void addUnvisitedUrl(String url){
		if(url != null)
			LinkQueue.addUnvisitedUrl(url);
		synchronized (test) {
//			test.notify();
			if(url != null)
				test.assign();
		}
	}

}
